package com.sc.auction.oss.controller;


import com.sc.auction.common.util.R;
import com.sc.auction.oss.service.OssService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@CrossOrigin
@RestController
@RequestMapping(value = "/oss/file")
public class OssController {
    @Autowired
    private OssService ossService;
    @ApiOperation(value = "文件上传")
    @PostMapping(value = "/upload")
    public R upload(@ApiParam(value = "文件",required = true) @RequestParam(value = "file") MultipartFile file,
                    @ApiParam(value = "模块",required = true) @RequestParam(value = "module") String module)throws Exception{
        InputStream inputStream=file.getInputStream();
        String originalFilename = file.getOriginalFilename();
        String uploadUrl = ossService.upload(inputStream, module, originalFilename);
        return R.ok().message("文件上传成功").data("url",uploadUrl);
    }
    @ApiOperation("删除文件")
    @GetMapping("/delete")
    public R delete(String url){
        try {
            ossService.delete(url);
        } catch (Exception e) {
            return R.error();
        }
        return R.ok();
    }
}
